package com.zmn.plat.business.impl.notice;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.plat.business.interfaces.notice.NoticeBService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.notice.Notice;
import com.zmn.plat.model.entity.notice.NoticeQuery;
import com.zmn.plat.services.interfaces.notice.NoticeService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author linfeng
 * @description 公告BSerive实现类
 * @modifier
 * @since 2020/12/8 15:20
 **/
@Service
@Log4j2
public class NoticeBServiceImpl implements NoticeBService {
    @Resource
    private NoticeService noticeService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private DeptListRemoteService deptListRemoteService;

    @Override
    public void addOrModifyNotice(Notice notice) {
        Date now = DateUtil.getNow();
        String opertor = StaffAdminService.currentStaff().getRealName();
        Integer relateId = notice.getRelateId();

        // 初始化公告，主要对组织ID和部门ID进行处理
        initNotice(notice);

        if (NumberUtil.isNullOrZero(relateId)) {
            notice.setStatus(GlobalConsts.YES);
            notice.setCreater(opertor);
            notice.setCreateTime(now);
            notice.setUpdater(opertor);
            notice.setUpdateTime(now);
            noticeService.insert(notice);
        } else {
            notice.setUpdater(opertor);
            notice.setUpdateTime(now);
            noticeService.updateByKey(notice);
        }
    }

    @Override
    public Notice getStaffNotice(Integer deptId) {
        Integer parentId = GlobalConsts.YES;

        if (NumberUtil.isNullOrZero(deptId)) {
            return new Notice();
        }

        while(parentId != null && parentId != 0 && parentId != 1) {
            NoticeQuery query = new NoticeQuery();
            query.setDeptId(deptId);
            query.setStatus(GlobalConsts.YES);
            List<Notice> data = noticeService.listByQuery(query);
            if (CollectionUtil.isNotNullOrEmpty(data)) {
                return data.get(0);
            } else {
                ResponseDTO<DeptDRO> responseDTO = deptListRemoteService.getDept(deptId);
                if (responseDTO.isSuccess()) {
                    if (Objects.isNull(responseDTO.getData())) {
                        return new Notice();
                    }
                    parentId = responseDTO.getData().getParentId();
                    deptId = parentId;
                } else {
                    return new Notice();
                }
            }
        }

        return new Notice();
    }

    /**
     * 初始化公告：主要对orgId和deptId处理
     * 
     * @param notice
     */
    private void initNotice(Notice notice) {
        /**
         * 组织架构
         */
        int orgId = notice.getOrgId() != null ? notice.getOrgId() : 0;
        if (orgId <= GlobalConsts.TOP_ID) {
            orgId = NumberUtil.isNotNullOrZero(notice.getOrgTwoId()) ? notice.getOrgTwoId() : 0;
        }
        if (orgId <= GlobalConsts.TOP_ID) {
            orgId = NumberUtil.isNotNullOrZero(notice.getPlat()) ? notice.getPlat() : 0;
        }
        notice.setOrgId(orgId);

        /**
         * 所属部门
         */
        int deptId = notice.getDeptId() != null ? notice.getDeptId() : 0;
        if (deptId <= GlobalConsts.TOP_ID) {
            deptId = NumberUtil.isNotNullOrZero(notice.getDeptTwoId()) ? notice.getDeptTwoId() : 0;
        }
        if (deptId <= GlobalConsts.TOP_ID) {
            deptId = NumberUtil.isNotNullOrZero(notice.getDeptOneId()) ? notice.getDeptOneId() : 0;
        }
        notice.setDeptId(deptId);
    }
}